# Build recipe for mesa-gallium-xa (mesa with XA support).
#
# Copyright (c) 2017 Mateus P. Rodrigues <mprodrigues@dragora.org>.
# Copyright (c) 2017-2021 Matias Fonzo <selk@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Exit immediately on any error
set -e

program=mesa
version=21.2.5
release=1

# Re-define package name
pkgname=mesa-gallium-xa

# Define a category for the output of the package name
pkgcategory=xorg_mesa

tarname=${program}-${version}.tar.xz

# Remote source(s)
fetch=https://archive.mesa3d.org/$tarname

description="
A Mesa build with XA support.

This is mainly required for VMware supported architectures,
for example, for the \"xf86-video-vmware\" package, but it
also comes with the XA support.
"

homepage=https://www.mesa3d.org/
license=MIT

build()
{
    unpack "${tardir}/$tarname"

    cd "$srcdir"

    # To increment compatibility with Musl (Thanks to "Alpine Linux")
    patch -p1 < "${worktree}/patches/mesa/musl-fix-includes.patch"
    patch -p1 < "${worktree}/patches/mesa/add-use-elf-tls.patch"

    # To provide a few xdemos (Thanks to BLFS!)
    patch -p1 < "${worktree}/patches/mesa/mesa-21.2.4-add_xdemos-1.patch"

    MESA_GIT_SHA1_OVERRIDE='53b2b224dc2de982c37915a0ad218e33365ff75e'
    export MESA_GIT_SHA1_OVERRIDE
    python3 bin/git_sha1_gen.py --output include/git_sha1.h

    # Set sane permissions
    chmod -R u+w,go-w,a+rX-s .

    case $arch in
    amd64 | x32 | i?86 )
        _extra_options="-Dglx-read-only-text=true"
        ;;
    esac

    rm -rf BUILD
    mkdir BUILD
    cd BUILD

    CPPFLAGS="$QICPPFLAGS -D_XOPEN_SOURCE=700" \
    CFLAGS="$QICFLAGS" CXXFLAGS="$QICXXFLAGS" LDFLAGS="$QILDFLAGS" \
    meson setup $configure_args \
     --libdir /usr/lib${libSuffix} \
     --buildtype=release \
     --strip \
     -Dplatforms="" \
     -Ddri-drivers="" \
     -Dgallium-drivers="svga" \
     -Dvulkan-drivers="" \
     -Dglx=disabled \
     -Degl=disabled \
     -Dosmesa=false \
     -Dgallium-xvmc=disabled \
     -Dgallium-va=disabled \
     -Dgallium-vdpau=disabled \
     -Dgles1=disabled \
     -Dgles2=disabled \
     -Dopengl=false \
     -Dgallium-nine=false \
     -Dvalgrind=disabled \
     -Dgallium-xa=enabled \
     $_extra_options \
     ..

    ninja -j${jobs}
    DESTDIR="$destdir" ninja -j${jobs} install

    unset -v MESA_GIT_SHA1_OVERRIDE _extra_options

    cd ../

    # Do not include config directory (provided by 'mesa')
    rm -rf "${destdir}/usr/share/drirc.d"

    # Compress and link man pages (if needed)
    if test -d "${destdir}/$mandir"
    then
        (
            cd "${destdir}/$mandir"
            find . -type f -exec lzip -9 {} +
            find . -type l | while read -r file
            do
                ln -sf "$(readlink -- "$file").lz" "${file}.lz"
                rm -- "$file"
            done
        )
    fi
}

